"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsrsasign_1 = __importDefault(require("jsrsasign"));
const uuid_1 = __importDefault(require("./uuid"));
const stringMd5_1 = __importDefault(require("./FSTool/stringMd5"));
let RSATool = {
    createKey(b = 2048) {
        let rsaKeypair = jsrsasign_1.default.KEYUTIL.generateKeypair('RSA', b);
        let publicDer = jsrsasign_1.default.KEYUTIL.getPEM(rsaKeypair.pubKeyObj);
        let privateDer = jsrsasign_1.default.KEYUTIL.getPEM(rsaKeypair.prvKeyObj, 'PKCS8PRV');
        return { privateDer, publicDer };
    },
    createJWT(ip, secure_key) {
        const tNow = jsrsasign_1.default.KJUR.jws.IntDate.get('now');
        const tEnd = jsrsasign_1.default.KJUR.jws.IntDate.get('now + 1day');
        const oPayload = {
            iss: 'WangShan',
            sub: 'NodeServer',
            nbf: tNow,
            iat: tNow,
            exp: tEnd,
            jti: (0, uuid_1.default)(),
            aud: ip
        };
        const sHeader = JSON.stringify({ alg: 'HS256', typ: 'JWT' });
        const sPayload = JSON.stringify(oPayload);
        return jsrsasign_1.default.KJUR.jws.JWS.sign('HS256', sHeader, sPayload, (0, stringMd5_1.default)(secure_key));
    },
    verifyJWT(jwt, ip, secure_key) {
        return jsrsasign_1.default.KJUR.jws.JWS.verifyJWT(jwt, (0, stringMd5_1.default)(secure_key), {
            alg: ['HS256'],
            aud: [ip],
            iss: ['WangShan'],
            gracePeriod: 300
        });
    }
};
exports.default = RSATool;
